package com.zhixiang.wbl.visitor.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zhixiang.wbl.visitor.R;
import com.zhixiang.wbl.visitor.Utils.DistanceUtil;
import com.zhixiang.wbl.visitor.model.home.HomeDate;
import com.zhixiang.wbl.visitor.model.home.Spot;
import com.zhixiang.wbl.visitor.model.home.Travelaround;

/**
 * Created by dongzhixiang on 16/5/18.
 */
public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
    private HomeDate mHomeDate;
    private Context mContext;
    private LayoutInflater mInflater;
    //建立枚举 3个item 类型
    public enum ITEM_TYPE {
        VIEW_TYPE_HEAD,//头部
        VIEW_TYPE_SPOT,//附近热门景点
        VIEW_TYPE_TRAVELAROUND//周边游
    }
    public HomeAdapter(HomeDate mHomeDate, Context mContext)
    {
        this.mHomeDate = mHomeDate;
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        if (viewType == ITEM_TYPE.VIEW_TYPE_HEAD.ordinal())
        {
           return new ViewHolderHead(mInflater
                   .inflate(R.layout.template_home_head,parent,false));
        }
        else if (viewType == ITEM_TYPE.VIEW_TYPE_SPOT.ordinal())
        {
           return new ViewHolderSpot(mInflater
                   .inflate(R.layout.template_spot,parent,false));
        }
        else if (viewType == ITEM_TYPE.VIEW_TYPE_TRAVELAROUND.ordinal())
        {
           return new ViewHolderTravelAround(mInflater
           .inflate(R.layout.template_travelaround,parent,false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
       if (holder instanceof ViewHolderHead)
       {
           ViewHolderHead holderHead = (ViewHolderHead)holder;
           if (0 == position)
           {
              holderHead.title.setText("附近热门景点");
           }
           else
           {
               holderHead.title.setText("周边游");
           }
       }
        else if (holder instanceof ViewHolderSpot)
       {
           ViewHolderSpot holderSpot = (ViewHolderSpot)holder;
           int spotCount = mHomeDate.getSpot().length;//获取附近景点的Item的个数
           int index = (position - 1)*3;
           Spot[] spots = mHomeDate.getSpot();
           int count = spots.length;
           for (int i=0;i<3;i++)
           {
              if (index+i<count)
              {
                  Spot spot = spots[index+i];
                  holderSpot.frameLayout[i].setVisibility(View.VISIBLE);
                  holderSpot.title[i].setText(spot.getName());
                  holderSpot.distance[i].setText(DistanceUtil.getDistanceString(spot.getDistance()));
                  Picasso.with(mContext).load(spot.getImageUrl()).into(holderSpot.imageView[i]);
              }
               else
              {
                  holderSpot.frameLayout[i].setVisibility(View.INVISIBLE);
              }
           }
       }
        else if (holder instanceof ViewHolderTravelAround)
       {
           ViewHolderTravelAround travelaroundHolder = (ViewHolderTravelAround) holder;
           int spotCount = mHomeDate.getSpot().length;//获取附近景点的Item的个数
           if (0 < spotCount%3)
               spotCount = spotCount/3+1;
           else
               spotCount = spotCount/3;
           Travelaround[] travelarounds = mHomeDate.getTravelaround();
           int count = mHomeDate.getTravelaround().length;
           int index = (position - 1 - spotCount - 1)*2;
           for (int i =0;i<2;i++)
           {
               if (index+i<count)
               {
                   Travelaround travelaround = travelarounds[index+i];
                   travelaroundHolder.linearLayout[i].setVisibility(View.VISIBLE);
                   travelaroundHolder.description[i].setText(travelaround.getDescription());
                   travelaroundHolder.price[i].setText("￥"+travelaround.getPrice());
                   travelaroundHolder.extra[i].setText(travelaround.getExtra());
                   Picasso.with(mContext).load(travelaround.getImageUrl()).into(travelaroundHolder.imageView[i]);
               }
               else
               {
                   travelaroundHolder.linearLayout[i].setVisibility(View.INVISIBLE);
               }
           }
       }
    }

    @Override
    public int getItemCount()
    {
        int spotCount = mHomeDate.getSpot().length;//获取附近景点的Item的个数
        if (0 < spotCount%3)
            spotCount = spotCount/3+1;
        else
            spotCount = spotCount/3;
        int travelCount = mHomeDate.getTravelaround().length;
        if (0< travelCount%2)
        {
            travelCount = travelCount/2 + 1;
        }
        else
        {
            travelCount = travelCount/2;
        }
        return 1+spotCount+1+travelCount;
    }

    @Override
    public int getItemViewType(int position)
    {
        int spotCount = mHomeDate.getSpot().length;//获取附近景点的Item的个数
        if (0 < spotCount%3)
            spotCount = spotCount/3+1;
        else
            spotCount = spotCount/3;
        int travelCount = mHomeDate.getTravelaround().length;
   /*     if (0< travelCount%2)
        {
            travelCount = travelCount/2 + 1;
        }
        else
        {
            travelCount = travelCount/2;
        }*/
        if (position ==0 ) return ITEM_TYPE.VIEW_TYPE_HEAD.ordinal();
        else if (position>=1&&position<=spotCount) return ITEM_TYPE.VIEW_TYPE_SPOT.ordinal();
        else if (position == spotCount+1) return ITEM_TYPE.VIEW_TYPE_HEAD.ordinal();
        else return ITEM_TYPE.VIEW_TYPE_TRAVELAROUND.ordinal();
    }

    class ViewHolderHead extends RecyclerView.ViewHolder
    {
        //设置附近热门景点头部
        TextView title;
        Button   button;
        //设置热门景点

        public ViewHolderHead(View itemView)
        {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.id_tv_title);
            button = (Button) itemView.findViewById(R.id.id_button_more);
        }
    }
    class ViewHolderSpot extends RecyclerView.ViewHolder
    {
        FrameLayout[] frameLayout = new FrameLayout[3];
        ImageView[] imageView= new ImageView[3];
        TextView[]  title = new TextView[3];
        TextView[]  distance = new TextView[3];
        public ViewHolderSpot(View itemView)
        {
            super(itemView);
           FrameLayout frameLayout1 = (FrameLayout)itemView.findViewById(R.id.id_fl1);
           ImageView imageView1 = (ImageView) itemView.findViewById(R.id.id_img1);
           TextView title1 = (TextView)itemView.findViewById(R.id.id_tv_title1);
           TextView distance1 = (TextView)itemView.findViewById(R.id.id_tv_distance1);

           FrameLayout frameLayout2 = (FrameLayout)itemView.findViewById(R.id.id_fl2);
           ImageView  imageView2 = (ImageView) itemView.findViewById(R.id.id_img2);
           TextView title2 = (TextView)itemView.findViewById(R.id.id_tv_title2);
           TextView distance2 = (TextView)itemView.findViewById(R.id.id_tv_distance2);

           FrameLayout frameLayout3 = (FrameLayout)itemView.findViewById(R.id.id_fl3);
           ImageView imageView3 = (ImageView) itemView.findViewById(R.id.id_img3);
           TextView title3 = (TextView)itemView.findViewById(R.id.id_tv_title3);
           TextView distance3 = (TextView)itemView.findViewById(R.id.id_tv_distance3);

           frameLayout[0] = frameLayout1;
           frameLayout[1] = frameLayout2;
           frameLayout[2] = frameLayout3;
            imageView[0] = imageView1;
            imageView[1] = imageView2;
            imageView[2] =imageView3;
            title[0] = title1;
            title[1] = title2;
            title[2] = title3;
            distance[0] =distance1;
            distance[1] = distance2;
            distance[2] =distance3;
        }
    }
    class ViewHolderTravelAround extends RecyclerView.ViewHolder
    {
        LinearLayout[] linearLayout = new LinearLayout[2];
        ImageView[] imageView = new ImageView[2];
        TextView[] description = new TextView[2];
        TextView[] extra = new TextView[2];
        TextView[] price = new TextView[2];
        public ViewHolderTravelAround(View itemView)
        {
            super(itemView);
            LinearLayout  linearLayout1 = (LinearLayout)itemView.findViewById(R.id.id_ll1);
            ImageView imageView1 = (ImageView)itemView.findViewById(R.id.id_img_spot1);
            TextView description1 = (TextView)itemView.findViewById(R.id.id_tv_title1);
            TextView extra1 = (TextView)itemView.findViewById(R.id.id_tv_extra1);
            TextView price1 = (TextView)itemView.findViewById(R.id.id_tv_price1);

            LinearLayout  linearLayout2 = (LinearLayout)itemView.findViewById(R.id.id_ll2);
            ImageView imageView2 = (ImageView)itemView.findViewById(R.id.id_img_spot2);
            TextView description2 = (TextView)itemView.findViewById(R.id.id_tv_title2);
            TextView extra2 = (TextView)itemView.findViewById(R.id.id_tv_extra2);
            TextView price2 = (TextView)itemView.findViewById(R.id.id_tv_price2);
            linearLayout[0] = linearLayout1;
            linearLayout[1] = linearLayout2;
            imageView[0] = imageView1;
            imageView[1] = imageView2;
            description[0] = description1;
            description[1] = description2;
            extra[0] = extra1;
            extra[1] = extra2;
            price[0] = price1;
            price[1] = price2;
        }
    }
}
